package com.bigfish.gbs.service.impl;

import com.bigfish.gbs.dao.UserMapper;
import com.bigfish.gbs.dao.UserRoleMapper;
import com.bigfish.gbs.entity.User;
import com.bigfish.gbs.entity.UserRole;
import com.bigfish.gbs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/* *
 * @Author Joycessh
 * @Description 
 * @Date 21:15 2018/3/17
 */
@Service("UserService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public String loadAccountRole(String appId) throws DataAccessException {

        return userMapper.selectUserRoles(appId);
    }

    @Override
    public List<User> getUserList() throws DataAccessException {
        return userMapper.selectUserList();
    }

    @Override
    public List<User> getUserListByRoleId(Integer roleId) throws DataAccessException {
        return userMapper.selectUserListByRoleId(roleId);
    }

    @Override
    public boolean authorityUserRole(String uid, int roleId) throws DataAccessException {
        UserRole userRole = new UserRole();
        userRole.setRoleId(roleId);
        userRole.setUserId(uid);
        userRole.setCreateTime(new Date());
        userRole.setUpdateTime(new Date());
        return userRoleMapper.insert(userRole) == 1? Boolean.TRUE :Boolean.FALSE;
    }

    @Override
    public boolean deleteAuthorityUserRole(String uid, int roleId) throws DataAccessException {
        UserRole userRole = new UserRole();
        userRole.setUserId(uid);
        userRole.setRoleId(roleId);
        return userRoleMapper.deleteByUniqueKey(userRole) == 1? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public User getUserByAppId(String appId) throws DataAccessException {

        return userMapper.selectByUniqueKey(appId);
    }

    @Override
    public List<User> getNotAuthorityUserListByRoleId(Integer roleId) throws DataAccessException {

        return userMapper.selectUserListExtendByRoleId(roleId);
    }

    @Override
    public List<User> getUserListByRoleIdAndName(Integer roleId, String name) {

        if (name.equals(null) & name.equals("")) {
            return userMapper.selectUserListByRoleId(roleId);
        }else {
            String likeName = '%' + name + '%';  // 为like模糊查询拼接sql通配符 '%'
            return userMapper.selectUserListByRoleIdAndName(roleId, likeName);
        }
    }

    @Override
    public boolean deleteUserByUserId(String userId) {
        int num = userMapper.deleteByPrimaryKey(userId);
        return num == 1? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public boolean updateMember(User user) {
        int num = userMapper.updateByPrimaryKeySelective(user);
        return num == 1? Boolean.TRUE : Boolean.FALSE;
    }
}
